<?php
namespace app\farm\controller;
use app\AdminRest;
use app\farm\model\Claim;
use app\farm\model\ClaimCollage;
use app\farm\model\ClaimOrder;
use app\farm\model\ClaimText;
use app\farm\model\CollageJoin;
use app\farm\model\Config;
use app\farm\model\Farmer;
use app\farm\model\LandCate;
use app\farm\model\LandOrder;
use app\farm\model\LandOrderSeed;
use app\farm\model\Machine;
use app\farm\model\Monitor;
use app\farm\model\MonitorText;
use app\farm\model\OrderAddress;
use app\farm\model\SendOrder;
use app\farm\model\Source;
use app\farm\model\User;
use app\shop\model\FreightTemplate;
use think\App;
use app\farm\model\Config as Model;
use think\facade\Db;


class AdminClaim extends AdminRest
{


    protected $model;


    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();


    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-10 17:10
     * @功能说明:领地 认养分类列表
     */
    public function claimCateList(){

        $input = $this->_param;

        $cate_model = new LandCate();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        $dis[] = ['type','=',$input['type']];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];

        }

        $data  = $cate_model->dataList($dis,$input['limit']);

        if(!empty($data['data'])){

            $farmer_model = new Farmer();

            foreach ($data['data'] as &$v){

                $v['farmer_title'] = $farmer_model->where('id','in',$v['farmer_id'])->where(['status'=>2])->column('title');

                $v['farmer_title'] = array_values($v['farmer_title']);
            }

        }

        return $this->success($data);
    }




    /**
     * @author chenniang
     * @DataTime: 2021-03-12 15:04
     * @功能说明:配置详情
     */
    public function claimCateInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $cate_model = new LandCate();

        $data  = $cate_model->dataInfo($dis);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-12 15:04
     * @功能说明:领地 认养分类编辑
     */
    public function claimCateUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $cate_model = new LandCate();

        $input['uniacid'] = $this->_uniacid;

        $data  = $cate_model->dataUpdate($dis,$input);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-10 17:13
     * @功能说明:领地 认养分类添加
     */
    public function claimCateAdd(){

        $input = $this->_input;

        $cate_model = new LandCate();

        $input['uniacid'] = $this->_uniacid;

        $data  = $cate_model->dataAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-28 11:26
     * @功能说明:用户下单列表
     */
    public function orderList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['pay_type'])){

            $dis[] = ['a.pay_type','=',$input['pay_type']];

        }

        if(!empty($input['order_code'])){

            $dis[] = ['a.order_code','like','%'.$input['order_code'].'%'];

        }

        if(!empty($input['goods_name'])){

            $dis[] = ['a.goods_name','like','%'.$input['goods_name'].'%'];

        }

        if(!empty($input['mobile'])){

            $dis[] = ['b.mobile','like','%'.$input['mobile'].'%'];

        }

        if(!empty($input['farmer_id'])){

            $dis[] = ['a.farmer_id','=',$input['farmer_id']];

        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $dis[] = ['a.create_time','between',"{$input['start_time']},{$input['end_time']}"];

        }

        $order_model  = new ClaimOrder();

        $farmer_model = new Farmer();

        $user_model   = new User();

        $data = $order_model->adminDataList($dis,$input['limit']);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['farmer_info'] = $farmer_model->dataInfo(['id'=>$v['farmer_id']],'title');

                $v['user_info']   = $user_model->dataInfo(['id'=>$v['user_id']]);

            }
        }


        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-28 11:52
     * @功能说明:用户下单详情
     */
    public function orderInfo(){

        $input = $this->_param;

        $farmer_model = new Farmer();

        $source_model = new Source();

        $dis = [

            'id' => $input['id']
        ];

        $order_model = new ClaimOrder();

        $data = $order_model->dataInfo($dis);

        $data['time_text']   = date('Y-m-d',$data['start_time']).'~'.date('Y-m-d',$data['end_time']);
        //农场信息
        $data['farmer_info'] = $farmer_model->dataInfo(['id'=>$data['farmer_id']]);
        //溯源信息
        $data['source'] = $source_model->dataInfo(['id'=>$data['source_id']]);

        $address_model = new OrderAddress();

        $data['address_info'] = $address_model->dataInfo(['order_id'=>$input['id'],'type'=>2]);

        if($data['is_collage']==1){

            $collage_model = new CollageJoin();

            $collage_info  = $collage_model->dataInfo(['order_id'=>$input['id']]);

            if(!empty($collage_info)){

                $data['collage_status'] = $collage_info['status'];

                $data['collage_discount_price'] = $data['init_price']-$collage_info['price'];

                $data['collage_discount_price'] = $data['collage_discount_price']>0?$data['collage_discount_price']:0;

            }

        }

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2022-02-08 17:14
     * @功能说明:配送订单列表
     */
    public function userSendOrderList(){

        $input = $this->_param;

        $send_order_model = new SendOrder();

        $data = $send_order_model->orderSendOrder($input['claim_order_id'],1,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-16 17:21
     * @功能说明:配送订单列表
     */
    public function sendOrderList(){

        $input = $this->_param;

        $send_order_model = new SendOrder();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['pay_time','>',0];

        if(!empty($input['pay_type'])){

            $dis[] = ['pay_type','=',$input['pay_type']];

        }

        if(!empty($input['type'])){

            $dis[] = ['type','=',$input['type']];

        }

        if(!empty($input['send_type'])){

            $dis[] = ['send_type','=',$input['send_type']];

        }
        if(!empty($input['farmer_id'])){

            $dis[] = ['farmer_id','=',$input['farmer_id']];

        }


        $data = $send_order_model->dataList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-09 14:00
     * @功能说明:配送订单详情
     */
    public function sendOrderInfo()
    {

        $input = $this->_param;

        $send_order_model = new SendOrder();

        $seed_model       = new LandOrderSeed;

        $farmer_model     = new Farmer();

        $send_order = $send_order_model->dataInfo(['id' => $input['id']]);

        if($send_order['type']==1){

            $order_model = new ClaimOrder();

            $send_order['claim_order'] = $order_model->dataInfo(['id' => $send_order['order_id']]);

        }else{

            $order_model = new LandOrder();

            $send_order['land_order'] = $order_model->dataInfo(['id' => $send_order['order_id']]);
            //种子列表
            $send_order['land_order']['seed'] = $seed_model->orderSeed($send_order['order_id']);
        }

        $send_order['farmer_info'] = $farmer_model->dataInfo(['id'=>$send_order['farmer_id']]);

        return $this->success($send_order);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-09 10:24
     * @功能说明:配送订单收货
     */
    public function sendOrderReceiving(){

        $input = $this->_input;

        $send_order_model = new SendOrder();

        $send_order = $send_order_model->dataInfo(['id'=>$input['id']]);

        if(empty($send_order)){

            $this->errorMsg('订单未找到');
        }

        if($send_order['pay_type']!=2&&$send_order['send_type']==1){

            $this->errorMsg('订单状态错误');
        }

        if($send_order['pay_type']!=3&&$send_order['send_type']==2){

            $this->errorMsg('订单状态错误');
        }

        $res = $send_order_model->sendOrderReceiving($send_order);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success(true);

    }



    /**
     * @author chenniang
     * @DataTime: 2022-02-09 09:47
     * @功能说明:发货订单发货
     */
    public function sendOrderSend(){

        $input = $this->_input;

        $send_order_model = new SendOrder();

        $send_order = $send_order_model->dataInfo(['id'=>$input['id']]);

        if(empty($send_order)){

            $this->errorMsg('订单未找到');
        }

        if($send_order['pay_type']!=2){

            $this->errorMsg('订单状态错误');
        }

        $config_model = new Config();

        $config = $config_model->dataInfo(['uniacid'=>$this->_uniacid]);

        $update = [

            'pay_type' => 3,

            'send_time'=> time(),

            'auto_receiving_time' => $config['auto_hx_time']*86400+time()

        ];

        Db::startTrans();

        $res = $send_order_model->dataUpdate(['id'=>$input['id']],$update);

        if($res==0){

            Db::rollback();

            $this->errorMsg('发货失败');
        }

        if($send_order['type']==1){

            $claim_order_model = new ClaimOrder();

            $claim_order_model->dataUpdate(['id'=>$send_order['order_id']],['pay_type'=>3]);
        }

        Db::commit();

        return $this->success(true);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-01 15:06
     * @功能说明:认养拼团列表
     */
    public function collageList(){

        $input = $this->_param;

        $collage_model = new ClaimCollage();

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status','>',-1];

        if(!empty($input['title'])){

            $dis[] = ['a.title','like','%'.$input['title'].'%'];
        }

        if(!empty($input['goods_name'])){

            $dis[] = ['b.title','like','%'.$input['goods_name'].'%'];
        }

        $data = $collage_model->adminDataList($dis,$input['limit']);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-01 15:11
     * @功能说明:添加拼团
     */
    public function collageAdd(){

        $input = $this->_input;

        $collage_model = new ClaimCollage();

        $input['uniacid'] = $this->_uniacid;

        $res = $collage_model->dataAdd($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-01 15:11
     * @功能说明:添加拼团
     */
    public function collageUpdate(){

        $input = $this->_input;

        $collage_model = new ClaimCollage();

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $res = $collage_model->dataUpdate($dis,$input);

        return $this->success($res);

    }



    /**
     * @author chenniang
     * @DataTime: 2022-07-01 15:11
     * @功能说明:添加拼团
     */
    public function collageInfo(){

        $input = $this->_param;

        $collage_model = new ClaimCollage();

        $dis = [

            'id' => $input['id']
        ];

        $res = $collage_model->dataInfo($dis);

        $claim_model = new Claim();

        $res['claim_title'] = $claim_model->where(['id'=>$res['claim_id']])->value('title');

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 15:28
     * @功能说明:认养管理列表
     */
    public function claimList(){

        $input = $this->_param;

        $claim_model = new Claim();

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(isset($input['status'])){

            $dis[] = ['status','=',$input['status']];

        }else{

            $dis[] = ['status','>',-1];
        }

        if(!empty($input['farmer_id'])){

            $dis[] = ['farmer_id','=',$input['farmer_id']];
        }

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $claim_model->dataList($dis,$input['limit']);

        if(!empty($data['data'])){

            $farmer_model = new Farmer();

            $cate_model = new LandCate();

            foreach ($data['data'] as &$v){

                $v['farmer_name'] = $farmer_model->where(['id'=>$v['farmer_id']])->value('title');

                $cate_name = $cate_model->where('id','in',$v['cate_id'])->column('title');

                $v['cate_name'] = implode(',',$cate_name);

            }

        }

        $arr = [

            0 => 'off_num',

            1 => 'on_num',
        ];

        foreach ($arr as $key => $value){

            $dis = [

                'uniacid' => $this->_uniacid,

                'status'  => $key
            ];

            $data[$value] = $claim_model->where($dis)->count();

        }
        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 16:32
     * @功能说明:添加认养
     */
    public function claimAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $claim_model = new Claim();

        $res = $claim_model->dataAdd($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 16:32
     * @功能说明:认养详情
     */
    public function claimInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $claim_model = new Claim();

        $res = $claim_model->dataInfo($dis);

        $claim_text_model  = new ClaimText();

        $monitor_text_model= new MonitorText();
        //流程
        $res['process'] = $claim_text_model->where(['claim_id'=>$input['id']])->select()->toArray();
        //监控
        $res['monitor'] = $monitor_text_model->where(['obj_id'=>$input['id'],'type'=>2])->column('monitor_id');

        $res['monitor'] = array_values($res['monitor']);

        $freightTemplate_model = new FreightTemplate();

        $res['send_template_type'] = $freightTemplate_model->where(['id'=>$res['send_tmpl_id']])->value('type');

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 16:32
     * @功能说明:编辑认养
     */
    public function claimUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $claim_model = new Claim();

        $res = $claim_model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 16:32
     * @功能说明:编辑认养
     */
    public function claimStatusUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $claim_model = new Claim();

        $res = $claim_model->where($dis)->update($input);

        return $this->success($res);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-12-31 15:11
     * @功能说明:认养和土地分类
     */
    public function landAndClaimCate(){

        $input = $this->_param;

        $dis = [

            'a.status' => 1,

            'a.uniacid'=> $this->_uniacid,

            'a.type'   => $input['type']
        ];

        $where = [];

        if(!empty($input['farmer_id'])){

            $where = [

                'a.is_public' => 1,

                'b.farmer_id' => $this->farmer['id']
            ];
        }


        $cate_model = new LandCate();

        $data = $cate_model->alias('a')
            ->join('lbfarm_land_cate_text b','a.id = b.cate_id','left')
            ->where($dis)
            ->where(function ($query) use ($where){
                $query->whereOr($where);
            })
            ->field('a.*')
            ->group('a.id')
            ->order('a.top desc,a.id desc')
            ->select()
            ->toArray();

        return $this->success($data);

    }





}
